package com.foreveross.crawl.vo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.alibaba.fastjson.JSONObject;
import com.foreveross.crawl.common.util.DateUtil;
import com.foreveross.taskservice.common.bean.TaskModel;
import com.foreveross.taskservice.common.model.AbstractChannelModel;
import com.foreveross.taskservice.common.model.airfreight.AirFreightChannelModel;

/**
 * 任务模型beta1版本
 * 
 * @author luofangyi
 * 
 */
public class TaskModelBeta1 implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6154836408609667793L;
	private String id;
	private String taskId;
	private Long channelId;
	private String channelName;
	/**
	 * 是否使用代理IP 0不使用，1使用
	 */
	private Long isProxyIp;
	/**
	 * 请求参数集 是一串JSON数据
	 */
	private String paramsJson;
	/**
	 * 描述监控页面显示的元数据
	 */
	private String metadata;
	/**
	 * 任务开始时间
	 */
	private String taskStartDate;
	/**
	 * 任务结束时间
	 */
	private String taskEndDate;
	/**
	 * 任务执行时间
	 */
	private long taskExcuteTotal;
	/**
	 * 页面编码方式 如:UTF-8等
	 */
	private String encoded;
	
	/**
	 * 代理IP，有多个则用  ~ 进行分割
	 */
	private String proxyIp;
	/**
	 * 任务发布时间
	 */
	private String taskPublish;

	/**
	 * 该任务类型（如高铁类型、航空类型等等）（与任务创建时间构建成独立的数据空间）
	 * 旧版任务模型而来
	 */
	private int taskType;

	public TaskModelBeta1() {

	}

	public TaskModelBeta1(String id, Long channelId, String channelName,
			Long isProxyIp, String paramsJson, String metadata,
			String taskStartDate, String taskEndDate) {
		this.taskId = id;
		this.channelId = channelId;
		this.channelName = channelName;
		this.isProxyIp = isProxyIp;
		this.paramsJson = paramsJson;
		this.metadata = metadata;
		this.taskStartDate = taskStartDate;
		this.taskEndDate = taskEndDate;
	}
	
	public TaskModelBeta1(String id, Long channelId, String channelName,
			Long isProxyIp, String paramsJson, String metadata,
			String taskStartDate, String taskEndDate, long taskExcuteTotal, String encoded,
			String taskPublish, String proxyIp, int taskType) {
		this.taskId = id;
		this.channelId = channelId;
		this.channelName = channelName;
		this.isProxyIp = isProxyIp;
		this.paramsJson = paramsJson;
		this.metadata = metadata;
		this.taskStartDate = taskStartDate;
		this.taskEndDate = taskEndDate;
		this.taskExcuteTotal = taskExcuteTotal;
		this.encoded = encoded;
		this.taskPublish = taskPublish;
		this.proxyIp = proxyIp;
		this.taskType = taskType;
	}


	/**  
	 * @Description: 适配旧版任务模型,将旧版任务转换为新任务
	 * @param oldModel
	 * @return
	 * @author luofangyi
	 * @date 2014-6-27 下午3:27:38 
	 */ 
	public static TaskModelBeta1 adapterTaskModel(TaskModel oldModel) {
		String flightType = obtainFlightType(oldModel);
		Metadata metaData = new Metadata(oldModel.getFromCity(),
				oldModel.getFromCityName(), oldModel.getToCity(),
				oldModel.getToCityName(), DateUtil.formatDay(
						oldModel.getCreateTime(), "yyyy-MM-dd HH:mm ss"),
				oldModel.getFlightDate(), oldModel.getReturnGrabDate(), flightType);
		
		String jsonStr = JSONObject.toJSONString(metaData);
		List<MetadataStructure> metadata = new ArrayList<MetadataStructure>();
		metadata.add(new MetadataStructure("fromCityName", "出发城市"));
		metadata.add(new MetadataStructure("toCityName", "到达城市"));
		metadata.add(new MetadataStructure("flightType", "航班类型"));
//		metadata.add(new MetadataStructure("createTime", "任务创建时间"));
		metadata.add(new MetadataStructure("flightDate", "航班日期"));
		if(oldModel.getIsReturn() == 1){//返程
			metadata.add(new MetadataStructure("returnGrabDate", "返程日期"));
		}

		String startDate = DateUtil.formatDay(new Date(), "HH时mm分ss秒");
		String endDate = "";
		//TODO 发布时间、页面编码方式(从新任务模板而来). 这两个属性主要用来展示，在旧任务模板中没有实际意义
		String taskPublish = DateUtil.formatDay(new Date(), "HH时mm分ss秒");
		String encoded = "UTF-8";
		
		return new TaskModelBeta1(oldModel.getId(), oldModel
				.getChannel().getId(), oldModel.getChannel().getName(), 1L,
				jsonStr, JSONObject.toJSON(metadata).toString(), startDate, endDate, 0l, encoded, taskPublish, oldModel.getProxyIp(), oldModel.getTaskType());
	}
	
	/**  
	 * @Description: 根据任务得到航班类型
	 * @param oldModel
	 * @return
	 * @author luofangyi
	 * @date 2014-7-18 下午3:40:49 
	 */ 
	public static String obtainFlightType(TaskModel oldModel){
		StringBuffer flightType = new StringBuffer("");
		switch (oldModel.getIsInternational()){
			case 0:
				flightType.append("国内");
				break;
			case 1:
				flightType.append("国际");
				break;
		}
		switch (oldModel.getIsReturn()){
			case 0:
				flightType.append("单程");
				break;
			case 1:
				flightType.append("往返");
				break;
		}
		return flightType.toString();
	}
	
	/**  
	 * @Description: 构建返回MQ时的任务模型，目前只包含部分字段
	 * @param newModel
	 * @return
	 * @author luofangyi
	 * @date 2014-7-3 下午6:57:07 
	 */ 
	public static TaskModel createTaskModelByRollbackTask(TaskModelBeta1 newModel) {
		
		TaskModel oldModel = new TaskModel();
		oldModel.setCreateTime(new Date());
		oldModel.setTaskType(newModel.getTaskType());
		oldModel.setId(newModel.getId());
		AbstractChannelModel channelModel = new AirFreightChannelModel();
		channelModel.setId(newModel.getChannelId());
		channelModel.setName(newModel.getChannelName());
		oldModel.setChannel(channelModel);
		
		
		return oldModel;
	}

	public String getId() {
		return taskId;
	}

	public void setId(String id) {
		this.taskId = id;
	}

	public Long getChannelId() {
		return channelId;
	}

	public void setChannelId(Long channelId) {
		this.channelId = channelId;
	}

	public static void main(String args[]) {
		TaskModel task = new TaskModel();
		task.setAttachHbaseKey("20017");
		task.setCreateTime(new Date());
		task.setFromCity("pek");
		task.setFromCityName("北京");
		task.setToCity("bkk");// 曼谷
		// task.setToCity("cnx");// 清迈
		// task.setToCity("pus");//釜山
		// task.setToCity("aal");//Aalborg 奥尔堡
		// task.setToCity("ath");//雅典
		task.setToCityName("曼谷");
		task.setFlightDate("2014-06-19");
		task.setReturnGrabDate("2014-06-28");
		task.setIsReturn(0);// 0:单程 1:往返
		task.setIsInternational(0);// 国际
		task.setId(UUID.randomUUID().toString());
		AbstractChannelModel channel = new AirFreightChannelModel();
		channel.setId(24L);
		channel.setName("泰航");
		task.setChannel(channel);

		System.out.println(JSONObject.toJSON(TaskModelBeta1
				.adapterTaskModel(task)));
	}

	public String getChannelName() {
		return channelName;
	}

	public void setChannelName(String channelName) {
		this.channelName = channelName;
	}

	public Long getIsProxyIp() {
		return isProxyIp;
	}

	public void setIsProxyIp(Long isProxyIp) {
		this.isProxyIp = isProxyIp;
	}

	public String getParamsJson() {
		return paramsJson;
	}

	public void setParamsJson(String paramsJson) {
		this.paramsJson = paramsJson;
	}

	public String getMetadata() {
		return metadata;
	}

	public void setMetadata(String metadata) {
		this.metadata = metadata;
	}

	public String getTaskStartDate() {
		return taskStartDate;
	}

	public void setTaskStartDate(String taskStartDate) {
		this.taskStartDate = taskStartDate;
	}

	public String getTaskEndDate() {
		return taskEndDate;
	}

	public void setTaskEndDate(String taskEndDate) {
		this.taskEndDate = taskEndDate;
	}

	@Override
	public String toString() {
		return "TaskModelBeta1 [id=" + id + ", taskId=" + taskId
				+ ", channelId=" + channelId + ", channelName=" + channelName
				+ ", isProxyIp=" + isProxyIp + ", paramsJson=" + paramsJson
				+ ", metadata=" + metadata + ", taskStartDate=" + taskStartDate
				+ ", taskEndDate=" + taskEndDate + ", taskExcuteTotal="
				+ taskExcuteTotal + ", encoded=" + encoded + ", proxyIp="
				+ proxyIp + ", taskPublish=" + taskPublish + ", taskType="
				+ taskType + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((channelId == null) ? 0 : channelId.hashCode());
		result = prime * result
				+ ((channelName == null) ? 0 : channelName.hashCode());
		result = prime * result + ((encoded == null) ? 0 : encoded.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result
				+ ((isProxyIp == null) ? 0 : isProxyIp.hashCode());
		result = prime * result
				+ ((metadata == null) ? 0 : metadata.hashCode());
		result = prime * result
				+ ((paramsJson == null) ? 0 : paramsJson.hashCode());
		result = prime * result + ((proxyIp == null) ? 0 : proxyIp.hashCode());
		result = prime * result
				+ ((taskEndDate == null) ? 0 : taskEndDate.hashCode());
		result = prime * result
				+ (int) (taskExcuteTotal ^ (taskExcuteTotal >>> 32));
		result = prime * result + ((taskId == null) ? 0 : taskId.hashCode());
		result = prime * result
				+ ((taskPublish == null) ? 0 : taskPublish.hashCode());
		result = prime * result
				+ ((taskStartDate == null) ? 0 : taskStartDate.hashCode());
		result = prime * result + taskType;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TaskModelBeta1 other = (TaskModelBeta1) obj;
		if (channelId == null) {
			if (other.channelId != null)
				return false;
		} else if (!channelId.equals(other.channelId))
			return false;
		if (channelName == null) {
			if (other.channelName != null)
				return false;
		} else if (!channelName.equals(other.channelName))
			return false;
		if (encoded == null) {
			if (other.encoded != null)
				return false;
		} else if (!encoded.equals(other.encoded))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (isProxyIp == null) {
			if (other.isProxyIp != null)
				return false;
		} else if (!isProxyIp.equals(other.isProxyIp))
			return false;
		if (metadata == null) {
			if (other.metadata != null)
				return false;
		} else if (!metadata.equals(other.metadata))
			return false;
		if (paramsJson == null) {
			if (other.paramsJson != null)
				return false;
		} else if (!paramsJson.equals(other.paramsJson))
			return false;
		if (proxyIp == null) {
			if (other.proxyIp != null)
				return false;
		} else if (!proxyIp.equals(other.proxyIp))
			return false;
		if (taskEndDate == null) {
			if (other.taskEndDate != null)
				return false;
		} else if (!taskEndDate.equals(other.taskEndDate))
			return false;
		if (taskExcuteTotal != other.taskExcuteTotal)
			return false;
		if (taskId == null) {
			if (other.taskId != null)
				return false;
		} else if (!taskId.equals(other.taskId))
			return false;
		if (taskPublish == null) {
			if (other.taskPublish != null)
				return false;
		} else if (!taskPublish.equals(other.taskPublish))
			return false;
		if (taskStartDate == null) {
			if (other.taskStartDate != null)
				return false;
		} else if (!taskStartDate.equals(other.taskStartDate))
			return false;
		if (taskType != other.taskType)
			return false;
		return true;
	}

	public String getEncoded() {
		return encoded;
	}

	public void setEncoded(String encoded) {
		this.encoded = encoded;
	}

	public String getTaskPublish() {
		return taskPublish;
	}

	public void setTaskPublish(String taskPublish) {
		this.taskPublish = taskPublish;
	}

	public int getTaskType() {
		return taskType;
	}

	public void setTaskType(int taskType) {
		this.taskType = taskType;
	}

	public String getProxyIp() {
		return proxyIp;
	}

	public void setProxyIp(String proxyIp) {
		this.proxyIp = proxyIp;
	}

	public long getTaskExcuteTotal() {
		return taskExcuteTotal;
	}

	public void setTaskExcuteTotal(long taskExcuteTotal) {
		this.taskExcuteTotal = taskExcuteTotal;
	}

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	

}

class Metadata implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9118372538285450361L;
	private String fromCity;
	private String fromCityName;
	private String toCity;
	private String toCityName;
	private String createTime;
	private String flightDate;
	/**
	 * 返程日期
	 */
	private String returnGrabDate;
	/**
	 * 航班类型    取值如：国内单程/往返  国际单程/往返
	 * 该值根据任务模板传过来的是否往返及是否国际来赋值
	 */
	private String flightType;

	public Metadata(String fromCity, String fromCityName, String toCity,
			String toCityName, String createTime, String flightDate,
			String returnGrabDate, String flightType) {
		super();
		this.fromCity = fromCity;
		this.fromCityName = fromCityName;
		this.toCity = toCity;
		this.toCityName = toCityName;
		this.createTime = createTime;
		this.flightDate = flightDate;
		this.returnGrabDate = returnGrabDate;
		this.flightType = flightType;
	}

	public String getFromCity() {
		return fromCity;
	}

	public void setFromCity(String fromCity) {
		this.fromCity = fromCity;
	}

	public String getFromCityName() {
		return fromCityName;
	}

	public void setFromCityName(String fromCityName) {
		this.fromCityName = fromCityName;
	}

	public String getToCity() {
		return toCity;
	}

	public void setToCity(String toCity) {
		this.toCity = toCity;
	}

	public String getToCityName() {
		return toCityName;
	}

	public void setToCityName(String toCityName) {
		this.toCityName = toCityName;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getFlightDate() {
		return flightDate;
	}

	public void setFlightDate(String flightDate) {
		this.flightDate = flightDate;
	}

	@Override
	public String toString() {
		return "Metadata [fromCity=" + fromCity + ", fromCityName="
				+ fromCityName + ", toCity=" + toCity + ", toCityName="
				+ toCityName + ", createTime=" + createTime + ", flightDate="
				+ flightDate + ", returnGrabDate=" + returnGrabDate
				+ ", flightType=" + flightType + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((createTime == null) ? 0 : createTime.hashCode());
		result = prime * result
				+ ((flightDate == null) ? 0 : flightDate.hashCode());
		result = prime * result
				+ ((flightType == null) ? 0 : flightType.hashCode());
		result = prime * result
				+ ((fromCity == null) ? 0 : fromCity.hashCode());
		result = prime * result
				+ ((fromCityName == null) ? 0 : fromCityName.hashCode());
		result = prime * result
				+ ((returnGrabDate == null) ? 0 : returnGrabDate.hashCode());
		result = prime * result + ((toCity == null) ? 0 : toCity.hashCode());
		result = prime * result
				+ ((toCityName == null) ? 0 : toCityName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Metadata other = (Metadata) obj;
		if (createTime == null) {
			if (other.createTime != null)
				return false;
		} else if (!createTime.equals(other.createTime))
			return false;
		if (flightDate == null) {
			if (other.flightDate != null)
				return false;
		} else if (!flightDate.equals(other.flightDate))
			return false;
		if (flightType == null) {
			if (other.flightType != null)
				return false;
		} else if (!flightType.equals(other.flightType))
			return false;
		if (fromCity == null) {
			if (other.fromCity != null)
				return false;
		} else if (!fromCity.equals(other.fromCity))
			return false;
		if (fromCityName == null) {
			if (other.fromCityName != null)
				return false;
		} else if (!fromCityName.equals(other.fromCityName))
			return false;
		if (returnGrabDate == null) {
			if (other.returnGrabDate != null)
				return false;
		} else if (!returnGrabDate.equals(other.returnGrabDate))
			return false;
		if (toCity == null) {
			if (other.toCity != null)
				return false;
		} else if (!toCity.equals(other.toCity))
			return false;
		if (toCityName == null) {
			if (other.toCityName != null)
				return false;
		} else if (!toCityName.equals(other.toCityName))
			return false;
		return true;
	}

	public String getReturnGrabDate() {
		return returnGrabDate;
	}

	public void setReturnGrabDate(String returnGrabDate) {
		this.returnGrabDate = returnGrabDate;
	}

	public String getFlightType() {
		return flightType;
	}

	public void setFlightType(String flightType) {
		this.flightType = flightType;
	}

	

}
